package com.caiheng.api.util;

/**
 * @author by  zhouwei
 * @createtime 2017/12/14 14:45
 * @功能：
 */
public class ByteUtil {


    private final static char[] mChars = "0123456789ABCDEF".toCharArray();
    private final static String mHexStr = "0123456789ABCDEF";

    /**
     * 字符串转换成十六进制字符串
     * @param str String 待转换的ASCII字符串
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     */
    public static String str2HexStr(String str){
        StringBuilder sb = new StringBuilder();
        byte[] bs = str.getBytes();

        for (int i = 0; i < bs.length; i++){
            sb.append(mChars[(bs[i] & 0xFF) >> 4]);
            sb.append(mChars[bs[i] & 0x0F]);
//            sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * 把字符串 转换成byte[]
     * @param s
     * @return
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return b;
    }

    /**
     * byte[]数组转换为16进制的字符串
     *
     * @param bytes 要转换的字节数组
     * @return 转换后的结果
     */
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * 获取 22 号命令的 写命令的开关的 请求数据
     * @param weiIndex
     * @param intDataIndex
     * @param OpenOrClose
     * @return
     */
    public static String getHexData(int weiIndex, int intDataIndex,String OpenOrClose){
        String s = "";
//        开 01160000  FFFF  0001
//        关 01160000  FFFE  0000
        //0000 0000 0000 0000
        if(OpenOrClose.equals("0")){//关闭
            for(int i = 0; i < intDataIndex; i++){
                if(weiIndex - 1 == i){
                    s+="0";
                }else{
                    s+="1";
                }
            }
        }else{//开启
            for(int i = 0; i < intDataIndex; i++){
                if(weiIndex - 1 == i){
                    s+="1";
                }else{
                    s+="0";
                }
            }
        }
        System.out.println("倒序之前："+s);
        s = StringUtil.daoxuStr(s);
        System.out.println("倒序之后："+s);
        s = binaryString2hexString(s).toUpperCase();
        if(OpenOrClose.equals("0")){//关闭
            s = s + "0000";
        }else{
            s = "FFFF" + s;
        }
        return s;
    }

    /**
     * 把 二进制数据 转换成16进制  字符串
     * @param bString
     * @return
     */
    public static String binaryString2hexString(String bString){
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuffer tmp = new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4)
        {
            iTmp = 0;
            for (int j = 0; j < 4; j++)
            {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }

    /**
     * 十六转二进制
     *
     * @param hex
     *            十六进制字符串
     * @return 二进制字符串
     */
    public static String hexStringToBinary(String hex) {
        hex = hex.toUpperCase();
        String result = "";
        int max = hex.length();
        for (int i = 0; i < max; i++) {
            char c = hex.charAt(i);
            switch (c) {
                case '0':
                    result += "0000";
                    break;
                case '1':
                    result += "0001";
                    break;
                case '2':
                    result += "0010";
                    break;
                case '3':
                    result += "0011";
                    break;
                case '4':
                    result += "0100";
                    break;
                case '5':
                    result += "0101";
                    break;
                case '6':
                    result += "0110";
                    break;
                case '7':
                    result += "0111";
                    break;
                case '8':
                    result += "1000";
                    break;
                case '9':
                    result += "1001";
                    break;
                case 'A':
                    result += "1010";
                    break;
                case 'B':
                    result += "1011";
                    break;
                case 'C':
                    result += "1100";
                    break;
                case 'D':
                    result += "1101";
                    break;
                case 'E':
                    result += "1110";
                    break;
                case 'F':
                    result += "1111";
                    break;
            }
        }
        return result;
    }
}
